<?php
class FrontCart
{
	public function __construct()
	{
		//session_register("cartid");
		if(!isset($_SESSION['cartid']))
		{
			srand();
			$_SESSION['cartid']=rand();
			$cartid=$_SESSION['cartid'];
		} else {
			$cartid=$_SESSION['cartid'];
		}
		
		$this->cartid=$cartid;
	}
	
	private function getQtyByItemCode($productid)
	{
		$sql = "SELECT quantity
				FROM tbl_shoppingcart
				WHERE product_id = '" . $productid . "' ";
		
		$rw = DB::fetchRow($sql);
		
		return $rw['quantity'];
	}
	
	public function addtoCart($productid, $qty, $price, $savecartid)
	{
		CommonFunc::loadFile("model/Common.php");
		CommonFunc::loadFile("model/Product.php");
		$sid = session_id();
		$common = new Common();
		$mod_product = new FrontProduct();
		
		if($savecartid != "")
		{
			$cartid = $savecartid;
		} else {
			$cartid = $this->cartid;
		}
		
		$sql = "SELECT *
	        	FROM tbl_shoppingcart
				WHERE product_id =" . $productid . " AND cart_id = " . $cartid . " ";
		$result = DB::fetchRow($sql);
		
		if($result)
		{
			$sql = "UPDATE tbl_shoppingcart 
					SET quantity = quantity + " . (int)($qty) . "
					WHERE cart_id = " . $cartid . " AND product_id = " . $productid . " ";			
		} else {
			$sql = "INSERT INTO tbl_shoppingcart (cart_id, product_id, price,date, quantity)
				VALUES (" . $cartid . ", " . $productid . ", " . $price . ", NOW(), " . $qty .")";			
		}
		$addcart = DB::exec($sql);
		if($addcart)
		{
			if($savecartid)
			{
				echo "<script>location.href='" . SITEURL . "index.php/?showsavecart/" . $savecartid . "'</script>";
			} else {
				echo "<script>location.href='" . SITEURL . "index.php/?showcart'</script>";
			}			
		}		
	}
	
	public function cartSummary()
	{
		if(isset($_SESSION['user_id']))
		{
			$userid = $_SESSION['user_id'];
		}
		$sql = "SELECT SUM(quantity*price) AS SUBTOTAL, SUM(quantity) AS ITEMQTY 
				FROM tbl_shoppingcart
				WHERE user_id = " . $userid;
		$cart_arr = DB::fetchRow($sql);
		return $cart_arr;		
	}
	
	public function getCart()
	{
		$sql = "SELECT C.cart_id, C.product_id, C.price, C.date, C.quantity, P.product_title,P.item_code
				FROM tbl_shoppingcart C
				LEFT JOIN tbl_products P ON C.product_id = P.productid 
				WHERE C.cart_id = '" . $this->cartid . "' ";
		
		$cart_arr = DB::fetchAll($sql);
		return $cart_arr;		
	}
	
	/**
	 * Update Quantity - ajax request
	 *
	 * @param unknown_type $productid
	 * @param unknown_type $qty
	 */
	
	public function updateCart($productid, $qty, $savecartid)
	{
		if($savecartid == "")
		{
			$cartid = $this->cartid;
		} else {
			$cartid = $savecartid;
		}
		
		$userid = CommonFunc::currentUserId();
		$sql = "UPDATE tbl_shoppingcart
				SET quantity = " . $qty . "
				WHERE product_id = " . $productid. "
				AND cart_id = " . $cartid . " ";
		DB::fetchAll($sql);
	}
	
	public function updateShoppingCart($cart_id)
	{
		$sql = "DELETE FROM tbl_shoppingcart
				WHERE cart_id = '" . $cart_id . "' ";
		DB::exec($sql);
	}
	
	public function delCart($productid)
	{
		$sql = "DELETE FROM tbl_shoppingcart
				WHERE product_id = '" . $productid . "' ";				
		
		DB::exec($sql);
	}
	
	public function saveCart($cart_id, $user_id, $cart_name)
	{
		$cart_name = $cart_name . "_" . date('Y-m-d');
		
		$sql1 = "SELECT COUNT(cart_id) AS CARTCOUNT 
				 FROM tbl_savecart
				 WHERE cart_name = '" . $cart_name . "'
				 AND user_id = " . $user_id;
		$cartarr = DB::fetchRow($sql1);
		//echo $cartarr['CARTCOUNT']; exit;
		if($cartarr['CARTCOUNT'] == 0)
		{
			$sql2 = "INSERT INTO tbl_savecart(cart_id, cart_name, user_id) 
					VALUES (" . $cart_id . ", '" . $cart_name . "', " . $user_id . ")";
			$savecart = DB::exec($sql2);
			if($savecart)
			{
				echo "Your cart has been saved";
			}			
		} else {		
			echo "<span class='error_txt1'>The name is already exists</span>";
		}
	}
	
	public function getSaveCartDetailsByUserId($userid)
	{
		$sql = "SELECT cart_id, cart_name
				FROM tbl_savecart
				WHERE user_id = " . $userid;
		
		$savecart_arr = DB::fetchAll($sql);
		
		return $savecart_arr;
	}
	
	public function getSaveCartDetailsByCartId($cartid)
	{
		$sql = "SELECT C.cart_id, C.product_id, C.price, C.date, C.quantity, P.product_title
				FROM tbl_shoppingcart C
				LEFT JOIN tbl_products P ON C.product_id = P.productid 
				LEFT JOIN tbl_savecart SC ON SC.cart_id = C.cart_id
				WHERE SC.cart_id = '" . $cartid . "' ";
		
		$savecart_arr = DB::fetchAll($sql);
		
		return $savecart_arr;
	}
	
	public function getCartDetailsByCartId($cartid)
	{
		$sql = "SELECT C.cart_id, C.product_id, C.price, C.date, C.quantity, P.product_title
				FROM tbl_shoppingcart C
				LEFT JOIN tbl_products P ON C.product_id = P.productid 
				WHERE C.cart_id = '" . $cartid . "' ";
		
		$savecart_arr = DB::fetchAll($sql);
		
		return $savecart_arr;
	}
	
	public function delSaveCart($savecartid)
	{
		echo $sql = "DELETE FROM tbl_savecart
				WHERE cart_id = '" . $savecartid . "' ";
		$delSaveCart = DB::exec($sql);
	}	
	
	public function updateProductQty($cartid)
	{
		$sql1 = "SELECT product_id, quantity
				 FROM tbl_shoppingcart
				 WHERE cart_id = '" . $cartid . "' ";
		$row1 = DB::fetchRow($sql1);
		
		$productid = $row1['product_id'];
		$quantity = $row1['quantity'];
		
		$sql2 = "UPDATE tbl_products SET quantity = quantity - " . (int)$quantity . "
				 WHERE productid=" . $productid;
		DB::exec($sql2);
	}
}
?>